#create image for pi presents that shows current weather conditions
#dunnsept at gmail dot com
##
# currently works for US locations, have not tested Canada eh.
# change URL and regex below for other weather sites/countries
import urllib,urllib2,re
from PIL import Image, ImageDraw, ImageFont


def main():

    try:
        ##modify below for your local folders/installation
        #I copied arial.ttf to this folder, you can also use
        #built-in fonts if you provide full path
        usrfont = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 75)
        tcolor=(0,0,0)
        ##modify below for your local folders/installation
        img = Image.open("/home/pi/weather/fall2.jpg")
        draw = ImageDraw.Draw(img)

        ### change the ZIPCODE below to match your local zip
        url='http://www.weather.com/weather/today/XXXXX'


        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0')
        response = urllib2.urlopen(req,timeout=30)
        link=response.read()
        response.close

        match2 = re.compile('<span itemprop="feels-like-temperature-fahrenheit">(.+?)</span>').findall(link)
        match3 = re.compile('<div class="wx-wind-label">(.+?)</div>').findall(link)
        match4 = re.compile('<div class="wx-phrase ">(.+?)</div>').findall(link)

        #edit text as you see fit, along with location on screen
        text2 = "My Town, My State. Current Temp= " + match2[0]
        text3 = match4[0] + " with winds " + match3[0]
        draw.text((100,400),text2,fill=tcolor,font=usrfont)
        draw.text((100,600),text3,fill=tcolor,font=usrfont)

        del draw

        ## change this for where you want it to go
        ## like /pp_home/pp_live_media
        img.save("/home/pi/pp_home/pp_live_tracks/weather.jpg")

    except Exception, e:
        print str(e)

        

main()
